package net.chenlin.dp.modules.mobile.controller;

import net.chenlin.dp.modules.sys.controller.AbstractController;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

@Controller
public class ClothesBorrow extends AbstractController {

    @Value("${myprop.uploadDir}")
    private String uploadDir;

    //跳转上传图片页面
    @RequestMapping(value = "/toUploadPage", method = RequestMethod.GET)
    public String toUploadPage() {
        return html("/mobile/upload");
    }
    //跳转主界面
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String toMainPage() {
        return html("/mobile/main");
    }


    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file,String ms,String selectTK,String selectXC) {
        try {
            //文件上传
            if (file!=null) {
                String filename = file.getOriginalFilename();
                String path = uploadDir + filename;
                File targetFile = new File(path);
                file.transferTo(targetFile);
            }else {
                return  "failed";
            }
        } catch (Exception e) {
            return  "failed";
        }
        return "success";
    }


    /**
     * 样衣借用跳转
     * @return
     */
    @RequestMapping(value = "/borrow", method = RequestMethod.GET)
    public String borrow() {
        System.out.println("======================borrow");
        return html("/mobile/borrow");
    }
}
